/*
 * CollectionDao.java
 *
 * Created on 4 de abril de 2006, 22:28
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package fmap.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;

/**
 *
 * @author javi
 */
public class TableDao
{
    
    /** Creates a new instance of CollectionDao */
    public TableDao ()
    {
    }
    
    
    public Vector selectIdsTable ()
    {
        Vector vIdTable = new Vector();
        try
        {
            Connection conn = ConnectionDao.getConnection ();
            PreparedStatement ps = conn.prepareStatement ( "SELECT idTable FROM fmap.tableNode ");
            
            ResultSet rs = ps.executeQuery ();

            while( rs.next () )
            {
                vIdTable.add ( rs.getInt ("idTable") );
            }

            rs.close ();
            ps.close ();
//            conn.close ();
            
        }
        catch( Exception e)
        {
            System.out.println ( e.getMessage () );
        }
        
        return vIdTable;
    }
    
    
    public int selectIdTableNewMapF ()
    {
        int idTable             = -1;
        int idTableMax          = this.selectMaxIdTable ();
        int numNodeMaxIdTable   = this.selectNumNode ( idTableMax );
        
        if( numNodeMaxIdTable > 20000 )
        {
            idTable = this.insertTable ();
            new fmap.dao.CreateDb().createTableNode ( idTable );
        }
        else
        {
            idTable = idTableMax;
        }
        
        System.out.println("ID MAX TABLA NODE:"+idTable);
        
        return idTable;
    }
    
    
    public int selectMaxIdTable ()
    {
        int idTable = -1;
        try
        {
            Connection conn = ConnectionDao.getConnection ();
            PreparedStatement ps = conn.prepareStatement ( "" +
                    "SELECT MAX(idTable) AS idTable" +
                    " FROM fmap.tableNode "
                    );
            
            ResultSet rs = ps.executeQuery ();
            
            if( rs.next () )
            {
                idTable = rs.getInt ("idTable");
            }
            
            rs.close ();
            ps.close ();
//            conn.close ();
            
        }
        catch( Exception e)
        {
            System.out.println ( e.getMessage () );
        }
        
        return idTable;
    }
    
    
    public int selectNumNode ( int idTable )
    {
        int numNode = -1;
        try
        {
            Connection conn = ConnectionDao.getConnection ();
            PreparedStatement ps = conn.prepareStatement ( "" +
                    "SELECT numNode " +
                    "FROM fmap.tableNode " +
                    "WHERE idTable=?");
            ps.setInt ( 1, idTable );
            
            ResultSet rs = ps.executeQuery ();
            
            if( rs.next () )
            {
                numNode = rs.getInt ("numNode");
            }
            
            rs.close ();
            ps.close ();
//            conn.close ();
            
        }
        catch( Exception e)
        {
            System.out.println ( e.getMessage () );
        }
        
        return numNode;
    }
    
    
    public int insertTable ()
    {
        int idTable = -1;
        try
        {
            Connection conn = ConnectionDao.getConnection ();
            
            PreparedStatement ps = conn.prepareStatement ( "INSERT INTO fmap.tableNode (numNode) VALUES(?)");
            
            ps.setInt (1, -1);
            
            ps.executeUpdate ();
            
            ps.close ();
            
            idTable = this.selectMaxIdTable ();
            
//            conn.close ();
        }
        catch( Exception e)
        {
            System.out.println ( e.getMessage () );
        }
        
        return idTable;
    }
    
    
    
    public void updateTable ( int idTable, int numNode )
    {
        
        try
        {
            Connection conn = ConnectionDao.getConnection ();
            
            PreparedStatement ps = conn.prepareStatement ( "" +
                    "UPDATE fmap.tableNode" +
                    " SET numNode=? " +
                    "WHERE idTable=?"
                    );
            
            ps.setInt (1, numNode );
            ps.setInt (2, idTable);
            
            ps.executeUpdate ();
            
            ps.close ();
//            conn.close ();
        }
        catch( Exception e)
        {
            System.out.println ( e.getMessage () );
        }
        
    }
    
    
    public void updateTableSumNode ( int idTable, int numNode )
    {
        
        int numNodeAntes = this.selectNumNode ( idTable );
        
        int numNodeAhora = numNodeAntes + numNode;
        
        try
        {
            Connection conn = ConnectionDao.getConnection ();
            
            PreparedStatement ps = conn.prepareStatement ( "" +
                    "UPDATE fmap.tableNode" +
                    " SET numNode=? " +
                    "WHERE idTable=?"
                    );
            
            ps.setInt (1, numNodeAhora );
            ps.setInt (2, idTable);
            
            ps.executeUpdate ();
            
            ps.close ();
//            conn.close ();
        }
        catch( Exception e)
        {
            System.out.println ( e.getMessage () );
        }
        
    }

    
    
    public boolean insertTableMapF (int idTable, int idMapF)
    {
        try
        {
            Connection conn = ConnectionDao.getConnection ();

            PreparedStatement ps = conn.prepareStatement ( "INSERT INTO fmap.tableNodeMapF (idTable, idMapF) VALUES(?, ?)");

            ps.setInt (1, idTable);
            ps.setInt (2, idMapF);
            
            ps.executeUpdate ();
            
            ps.close ();

//            conn.close ();
        }
        catch( Exception e)
        {
            System.out.println ( e.getMessage () );
        }
        
        return true;
    }

    
    
    /**
     * Obtiene los ids de las tablas en las que se almacena algun nodo para el mapa de ficheros pasado como argumento
     */
    public Vector selectIdsTableMapF ( int idMapF )
    {
        Vector vIdTable = new Vector();
        try
        {
            Connection conn = ConnectionDao.getConnection ();
            PreparedStatement ps = conn.prepareStatement ( "SELECT idTable FROM fmap.tableNodeMapF WHERE idMapF=?");

            ps.setInt (1, idMapF);

            ResultSet rs = ps.executeQuery ();

            while( rs.next () )
            {
                vIdTable.add ( rs.getInt ("idTable") );
            }

            rs.close ();
            ps.close ();
//            conn.close ();

        }
        catch( Exception e)
        {
            System.out.println ( e.getMessage () );
        }

        return vIdTable;
    }

}